package com.hna.iot.controller.business;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.iot.base.BaseController;
import com.hna.iot.common.result.BaseResult;
import com.hna.iot.model.*;
import com.hna.iot.service.IIotDeviceService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class DeviceController extends BaseController {

	@Value("${photo_dir}")
	public  String photoDir;
	
	@Autowired
	IIotDeviceService deviceService;

	

	private final static Logger log = LoggerFactory.getLogger(DeviceController.class);

	@RequestMapping("/device/index")
	public String index() {
		return "pages/device/index";
	}

	@RequestMapping("/device/add")
	public String add(HttpServletRequest request,String name) {
		request.setAttribute("name", name);
		return "pages/device/add";
	}

	

    @RequestMapping("/device/detail")
    public ModelAndView details(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("pages/device/detail");
        modelAndView.addObject("id", request.getParameter("id"));
        return modelAndView;
    }
    
    
    @RequestMapping("/device/edit")
	public ModelAndView edit(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("pages/device/edit");
		modelAndView.addObject("id", request.getParameter("id"));
		return modelAndView;
	}

	@RequestMapping(value = { "/rest/device/list" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> list(HttpServletRequest request, PageInfo pageInfo, IotDevice device,String deviceName) {
		Map<String, Object> res = new HashMap<>();
		try {
			Wrapper<IotDevice> wrapper = new EntityWrapper<>();
//			wrapper.eq("name",name);
			if(StringUtils.isNotEmpty(deviceName)) {
				wrapper.eq("device_name",deviceName);
			}
			device = deviceService.selectOne(wrapper);
			Page<IotDevice> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<IotDevice> deviceList = deviceService.selectPage(page, wrapper);

			res.put("rows", deviceList.getRecords());
			res.put("total", deviceList.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}

	@RequestMapping("/rest/device/info")
	public @ResponseBody BaseResult info(HttpServletRequest request) {
		Long id = Long.parseLong(request.getParameter("id"));
		IotDevice device = deviceService.selectById(id);
		return BaseResult.success(device);
	}

	@RequestMapping(value = { "/rest/device/del" }, method = RequestMethod.POST)
	public @ResponseBody BaseResult del(HttpServletRequest request, String ids) {
		try {

			if (StringUtils.isEmpty(ids)) {
				return BaseResult.success();
			}
			List<String> asList = Arrays.asList(ids.split(","));
			deviceService.deleteBatchIds(asList);
//			for (String id: asList
//				 ) {
//				this.syninfo(Long.valueOf(id),2);
//			}
			return BaseResult.success();
		} catch (Exception e) {
			log.error("发生异常了", e);
			return BaseResult.fail(e.getMessage());
		}
	}

	@RequestMapping(value = { "/rest/device/add" }, method = RequestMethod.POST)
	public @ResponseBody BaseResult add(HttpServletRequest request, IotDevice device) {
		Map<String, Object> res = new HashMap<>();
		try {
//        	device.setMeterNo(meterNo);
			deviceService.insert(device);

			return BaseResult.success();
		} catch (Exception e) {
			log.error("发生异常了", e);
			return BaseResult.fail(e.getMessage());
		}
	}

	@RequestMapping(value = { "/rest/device/update" }, method = RequestMethod.POST)
	public @ResponseBody BaseResult update(HttpServletRequest request, IotDevice device, String meterNo) {
		Map<String, Object> res = new HashMap<>();
		try {
//        	device.setMeterNo(meterNo);
			deviceService.updateById(device);

			return BaseResult.success();
		} catch (Exception e) {
			log.error("发生异常了", e);
			return BaseResult.fail(e.getMessage());
		}
	}
	
	
	

}
